package com.hyg.oa.overtime.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hyg.oa.common.DateUtil;
import com.hyg.oa.common.PageUtils;
import com.hyg.oa.common.Pagination;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyg.oa.common.UUIDUtils;
import com.hyg.oa.overtime.model.Overtime;
import com.hyg.oa.overtime.service.OvertimeService;


/**
 * Controller类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/overtime") 
public class OvertimeController {
	
	@Autowired
	private OvertimeService overtimeService;
	
	/**
	 * page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		//总数
		long totalCount = this.overtimeService.findTotalCount();
		//page对象
		Pagination pagination = PageUtils.create(request, totalCount);
		
		//数据
		List<Overtime> list = this.overtimeService.findPage(pagination.getPageNo(), pagination.getPageSize());
		model.put("list", list);
		model.put("pagination", pagination);
		
		return new ModelAndView("/jsp/overtime/list.jsp", model);
	}
	
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		Map<String, Object> model = new HashMap<String,Object>();
		
		List<Overtime> list = this.overtimeService.findAll();
		model.put("list", list);
		
		return new ModelAndView("/jsp/overtime/list.jsp", model);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("/jsp/overtime/add.jsp");
	}
	
	
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String username = request.getParameter("username");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String length = request.getParameter("length");
			String content = request.getParameter("content");
			String viewer = request.getParameter("viewer");
			String send = request.getParameter("send");
			
			if(StringUtils.isEmpty(send)) {
				send = "0";
			}
		
			Overtime overtime = new Overtime();
			overtime.setUsername(username);
			overtime.setStartTime(DateUtil.toDate(startTime, "yyyy-MM-dd"));
			overtime.setEndTime(DateUtil.toDate(endTime, "yyyy-MM-dd"));
			overtime.setLength(Float.parseFloat(length));
			overtime.setContent(content);
			overtime.setViewer(viewer);
			overtime.setSend(send);
			overtime.setCreatetime(new Date());
			overtime.setId(UUIDUtils.generate());
			this.overtimeService.save(overtime);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * 用户编辑
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		Overtime overtime = this.overtimeService.findById(id);
		model.put("overtime", overtime);
		
		return new ModelAndView("/jsp/overtime/edit.jsp", model);
	}
	
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			String username = request.getParameter("username");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String length = request.getParameter("length");
			String content = request.getParameter("content");
			String viewer = request.getParameter("viewer");
			String send = request.getParameter("send");
			
			Overtime overtime = this.overtimeService.findById(id);
			overtime.setUsername(username);
			overtime.setStartTime(DateUtil.toDate(startTime, "yyyy-MM-dd"));
			overtime.setEndTime(DateUtil.toDate(endTime, "yyyy-MM-dd"));
			overtime.setLength(Float.parseFloat(length));
			overtime.setContent(content);
			overtime.setViewer(viewer);
			overtime.setSend(send);
			overtime.setUpdatetime(new Date());
			this.overtimeService.update(overtime);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}

	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Object del(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			this.overtimeService.delete(id);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
}
